package com.huilian.iotbox.data.service.impl;

import com.huilian.iotbox.data.dao.CommodityCommonDao;
import com.huilian.iotbox.data.dto.CommodityDto;
import com.huilian.iotbox.data.dto.CommodityStockDto;
import com.huilian.iotbox.data.enums.CommodityStockRecordFirstTypeEnum;
import com.huilian.iotbox.data.service.CommodityCommonService;
import com.huilian.iotbox.data.service.CommodityStockCommonService;
import com.huilian.iotbox.data.service.CommoditySupplierCommonService;
import com.huilian.iotbox.data.vo.CommodityStockVo;
import com.huilian.iotbox.data.vo.CommoditySupplierVo;
import com.huilian.iotbox.data.vo.CommodityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommodityCommonServiceImpl implements CommodityCommonService {
    @Autowired
    private CommodityCommonDao commodityDao;
    @Resource
    private CommoditySupplierCommonService commoditySupplierService;
    @Resource
    private CommodityStockCommonService commodityStockService;

    @Override
    public List<CommodityVo> findListById(CommodityDto commodityDto) {
        List<CommodityVo> list = commodityDao.findGoodsById(commodityDto);
        List<CommoditySupplierVo> commoditySupplierVos = commoditySupplierService.getSupplierByUserId(commodityDto.getUserInfo().getMchStoreUserId());
        for (CommodityVo commodityVo : list) {
            commodityVo.getCommoditySupplierVos().addAll(commoditySupplierVos);
            CommodityStockDto commodityStockDto = new CommodityStockDto();
            commodityStockDto.setCommodityId(commodityVo.getId());
            commodityStockDto.setMerchantStoreId(commodityDto.getUserInfo().getMchStoreId());
            CommodityStockVo commodityStockVo = commodityStockService.findStockV2(commodityStockDto);
            commodityVo.setStock(commodityStockVo.getStock());
        }
        return list;
    }

}
